package com.iseastar.guojiang.team;

import android.opengl.ETC1;
import android.os.Bundle;
import android.os.Message;
import android.text.InputFilter;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.view.DeleteEditText;
import com.shouhuobao.station.R;

import droid.frame.view.InputFilterExt;
import droid.frame.view.TextWatcherExt;

/**
 * 创建战队
 * Created by lidandan on 2018/6/12.
 */
public class CreateCourierTeamActivity extends BaseActivity2 implements View.OnClickListener {
    private DeleteEditText mTeamNameE, mTeamIntroduceE, mTeamManifestoTV;
//    private CheckBox mProtocolCheckbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.create_courier_team_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("创建战队");
        mTeamNameE = (DeleteEditText) findViewById(R.id.team_name_tv);
        mTeamManifestoTV = (DeleteEditText) findViewById(R.id.team_manifesto_tv);
        mTeamIntroduceE = (DeleteEditText) findViewById(R.id.team_introduce_tv);
//        mProtocolCheckbox = (CheckBox) findViewById(R.id.protocol_checkbox);
        TextView mGiveUpTV = (TextView) findViewById(R.id.give_up_tv);
        TextView mCreateTV = (TextView) findViewById(R.id.create_tv);
        mGiveUpTV.setOnClickListener(this);
        mCreateTV.setOnClickListener(this);

        mTeamNameE.setFilters(new InputFilter[]{new InputFilterExt(4)});
        mTeamNameE.addTextChangedListener(new TextWatcherExt() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 10) {
                    mTeamNameE.setText(s.toString().substring(0, 10));
                    mTeamNameE.setSelection(mTeamNameE.getText().toString().length());
                }
            }
        });

        mTeamManifestoTV.setFilters(new InputFilter[]{new InputFilterExt(4)});
        mTeamManifestoTV.addTextChangedListener(new TextWatcherExt() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 20) {
                    mTeamManifestoTV.setText(s.toString().substring(0, 20));
                    mTeamManifestoTV.setSelection(mTeamManifestoTV.getText().toString().length());
                }
            }
        });

        mTeamIntroduceE.setFilters(new InputFilter[]{new InputFilterExt(4)});
        mTeamIntroduceE.addTextChangedListener(new TextWatcherExt() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 200) {
                    mTeamIntroduceE.setText(s.toString().substring(0, 200));
                    mTeamIntroduceE.setSelection(mTeamIntroduceE.getText().toString().length());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //放弃
            case R.id.give_up_tv:
                finish();
                break;
            //立即创建
            case R.id.create_tv:
                if (isEmpty(mTeamNameE.getText().toString())) {
                    showToast("战队名称不能为空");
                    return;
                }
                if (mTeamNameE.getText().toString().length() < 4) {
                    showToast("战队名称不小于4个字");
                    return;
                }
                if (isEmpty(mTeamManifestoTV.getText().toString())) {
                    showToast("战队宣言不能为空");
                    return;
                }
                if (mTeamManifestoTV.getText().toString().length() < 5) {
                    showToast("战队宣言不小于5个字");
                    return;
                }
                if (isEmpty(mTeamIntroduceE.getText().toString())) {
                    showToast("战队介绍不能为空");
                    return;
                }

//                if (!mProtocolCheckbox.isChecked()) {
//                    showToast("请阅读并同意XXXXXXXX");
//                    return;
//                }
                showLoadingDialog(null);
                AppHttp.getInstance().teamCreate(mTeamNameE.getText().toString(), mTeamManifestoTV.getText().toString(), mTeamIntroduceE.getText().toString());
                break;
//            case R.id.protocol_checkbox:
//                if (mProtocolCheckbox.isChecked()) {
//                    mProtocolCheckbox.setChecked(false);
//                } else {
//                    mProtocolCheckbox.setChecked(true);
//                }
//                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what){
            //创建战队返回
            case MsgID.courier_team_create:
                cancelLoadingDialog();
                ReqResult<?> reqResult = JSON.parser(msg.obj);
                if (checkLoginStatus(reqResult)){
                    finish();
                }else {
                    showToast(reqResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }
}
